package com.durian.base.net;

import okhttp3.Headers;
import okhttp3.Response;

/**
 * 说明：HttpResponse
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019/1/31 14:14
 * <p/>
 * 版本：version 1.0
 */
public class HttpResponse {

    private String taskId;
    private String url;
    private HttpRequestParams mRequestParams;

    private Response  response;
    private Headers   headers;
    private boolean   isSuccess;
    private String    result;
    private int       errorCode;
    private int       httpCode;
    private Exception exception;
    private boolean fromCache = false;

    private HttpResponse(){ }

    private HttpResponse(int errorCode,Exception e){
        this.isSuccess = false;
        this.errorCode = errorCode;
        this.exception = e;
    }

    public static HttpResponse download(Response response){
        if (response == null){
            return nullResponse();
        }else {
            try {
                if (response.isSuccessful()){
                    HttpResponse httpResponse = new HttpResponse();
                    httpResponse.response = response;
                    httpResponse.httpCode = response.code();
                    httpResponse.isSuccess = true;
                    httpResponse.headers = response.headers();
                    return httpResponse;
                }else {
                    if (response.code() == 504){
                        return timeOut(null);
                    }else {
                        return new HttpResponse(response.code(), new RuntimeException(response.message()));
                    }
                }
            }catch (Exception e){
                Utils.eLog(e);
                return unkown(e);
            }
        }
    }

    public static HttpResponse fromCache(HttpResponse response ,String result){
        if (response == null){
            response = new HttpResponse();
        }
        response.result = result;
        response.fromCache = true;
        return response;
    }

    public static HttpResponse response(Response response){
        if (response == null){
            return nullResponse();
        }else {
            try {
                String result = response.body().string();

                if (response.isSuccessful()){
                    if (result == null){
                        return nullResponse();
                    }else {
                        HttpResponse httpResponse = new HttpResponse();
                        httpResponse.result = result;
                        httpResponse.httpCode = response.code();
                        httpResponse.isSuccess = true;
                        httpResponse.headers = response.headers();
                        return httpResponse;
                    }
                }else {
                    if (response.code() == 504){
                        return timeOut(null);
                    }else {
                        return new HttpResponse(response.code(), new RuntimeException(response.message()));
                    }
                }
            }catch (Exception e){
                Utils.eLog(e);
                return unkown(e);
            }
        }
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setRequestParams(HttpRequestParams requestParams) {
        mRequestParams = requestParams;
    }

    public String getTaskId() {
        return taskId;
    }

    public String getUrl() {
        return url;
    }

    public HttpRequestParams getRequestParams() {
        return mRequestParams;
    }

    public static HttpResponse nullResponse(){
        return new HttpResponse(HttpConfig.ErrorCode.RESPONSE_IS_NULL, new RuntimeException("response is null!"));
    }

    public static HttpResponse timeOut(Exception e){
        return new HttpResponse(HttpConfig.ErrorCode.TIME_OUT, e == null ? new RuntimeException("response time out!") : e);
    }

    public static HttpResponse unkown(Exception e){
        return new HttpResponse(HttpConfig.ErrorCode.UNKNOW, e == null ? new RuntimeException("response error!") : e);
    }

    public boolean isSuccess(){
        return isSuccess;
    }

    public String getResult() {
        return result;
    }

    public Exception getException() {
        return exception;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public Headers getHeaders() {
        return headers;
    }

    public int getHttpCode() {
        return httpCode;
    }

    public boolean isFromCache(){
        return fromCache;
    }

    public Response getResponse() {
        return response;
    }
}
